home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7529 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: yarrina.connect.com.au!usenet
  2. From: Andrew Dalgleish <andrewd@axonet.com.au>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: HELP: Illegal Pointer Arithmetic
  5. Date: Tue, 27 Feb 1996 18:52:17 +1000
  6. Organization: Axon Research, Pty Ltd
  7. Message-ID: <3132C641.1D1B@axonet.com.au>
  8. References: <4gj0ug$730@news.one.net> <danpop.825121139@rscernix> <31306a18.309961701@nntp.ix.netcom.com> <4gq77b$bmo@reznor.larc.nasa.gov> <4gqija$bmo@reznor.larc.nasa.gov>
  9. NNTP-Posting-Host: 203.63.6.118
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win95; I)
  14.  
  15. Ed Hook wrote:
  16. >   Blair Houghton gently pointed out to me (_via_ e-mail) that the above
  17. >   defense of Schildt is *erroneous*, since the code in question fails on
  18. >   an _empty_ input file. Overall, I feel somehow _cleaner_ now ...
  19.  
  20.     do {
  21.         ch = fgetc(fp);
  22.         /* ... */
  23.     } while (!feof(fp));
  24.  
  25. This may fail for more than just empty files!
  26. The function fgetc() returns an int.
  27. If ch is a char it fails.
  28. If ch is an int, AND you test for EOF, it's ok.
  29. The old problem of trying to return two things in one value.
  30.  
  31.     int    iChar;    /* int to hold EOF */
  32.     do {
  33.         /* get char cast to int or EOF if at end-of-file */
  34.         iChar = fgetc(fp);
  35.         /* Test for end-of-file */
  36.         if (iChar != EOF) {
  37.             /* ... */
  38.         }
  39.     } while (!feof(fp));
  40.  
  41. I wish my mailer had bracket matching and syntax checking :)
  42.  
  43. -- 
  44. Andrew Dalgleish
  45. Senior Software Engineer
  46. Axon Research, Pty Ltd
  47. 6 Wallace Ave
  48. Toorak, VIC
  49. 3142
  50. AUSTRALIA
  51. Tel:    +61-3-9826-5538
  52. Fax:    +61-3-9824-0083
  53. Email:    andrewd@axonet.com.au
  54.